import { useThemeType } from "@/hooks";
import { useMemo } from "react";

const rgb = Array(3).fill(null);
// 是否是有效的色彩空间
const isValidAlpha = (alpha: number) => alpha >= 0 && alpha <= 1;

export const useTextColor = (
  alpha: number,
  { darkSpace = 255, lightSpace = 0 }: iParmas = {}
): useTextColorType => {
  const navTheme = useThemeType();
  const colorValue = navTheme === "realDark" ? darkSpace : lightSpace;

  return useMemo(() => {
    let value = 1;

    if (isValidAlpha(alpha as number)) {
      value = alpha as number;
    }

    const colorComposition = (_alpha: number) => {
      let _value = value;
      if (isValidAlpha(_alpha)) {
        _value = _alpha;
      }
      return `rgba(${rgb.map((_) => colorValue).join(",")},${_value})`;
    };

    return [colorComposition(value), colorComposition];
  }, [colorValue, alpha, darkSpace, lightSpace]);
};

interface iParmas {
  darkSpace?: number;
  lightSpace?: number;
}

type useTextColorType = [string, (alpha: number) => string];
